package com.xiaoshuidi.cloud.module.member.service.topics;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicListVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicUpdateStatusVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicsCreateVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicsDetailVo;
import com.xiaoshuidi.cloud.module.member.controller.admin.topics.vo.TopicsPageVO;
import com.xiaoshuidi.cloud.module.member.mapper.topics.MemberTopicsMapper;
import com.xiaoshuidi.cloud.module.member.pojo.topics.MemberTopics;
import com.xiaoshuidi.cloud.module.member.pojo.topics.MemberTopicsPost;
import com.xiaoshuidi.cloud.module.member.repository.MemberTopicsPostRepository;
import com.xiaoshuidi.cloud.module.member.repository.MemberTopicsRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.xiaoshuidi.cloud.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.xiaoshuidi.cloud.module.member.enums.ErrorCodeConstants.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class MemberTopicsServiceImpl extends ServiceImpl<MemberTopicsMapper, MemberTopics> implements MemberTopicsService {

    private final MemberTopicsRepository memberTopicsRepository;

    private final MemberTopicsPostRepository memberTopicsPostRepository;

    @Override
    public Boolean create(TopicsCreateVO createReqVO) {
        memberTopicsRepository.create(createReqVO);
        return true;
    }

    @Override
    public Boolean edit(TopicsCreateVO createReqVO) {
        memberTopicsRepository.edit(createReqVO);
        return true;
    }

    @Override
    public TopicsDetailVo getById(Long id) {
        MemberTopics byId = memberTopicsRepository.getById(id);
        TopicsDetailVo vo = new TopicsDetailVo();
        BeanUtils.copyProperties(byId, vo);
        //获取点赞数总和
        int thumbs = memberTopicsPostRepository.getTopicThumbs(id);
        vo.setThumbs(thumbs);
        Integer topicStatus = memberTopicsRepository.getTopicStatus(byId);
        vo.setTopicStatus(topicStatus);
        return vo;
    }

    @Override
    public PageResult<TopicListVO> getPage(TopicsPageVO pageVO) {
        return memberTopicsRepository.getPage(pageVO);
    }

    @Override
    public PageResult<TopicListVO> getAppPage(TopicsPageVO pageVO) {
        IPage<MemberTopics> page = memberTopicsRepository.getAppPage(pageVO);
        //获取所有的话题ids
        List<MemberTopics> records = page.getRecords();
        List<Long> ids = records.stream().map(MemberTopics::getId).collect(Collectors.toList());
        List<TopicListVO> collect = null;
        if (ids != null && ids.size() > 0) {
            Map<Long, List<MemberTopicsPost>> recordsMap = memberTopicsPostRepository.getRecordsMapByTopicIds(ids);
            collect = records.stream().map(t -> {
                TopicListVO vo = new TopicListVO();
                BeanUtils.copyProperties(t, vo);
                List<MemberTopicsPost> memberTopicsPosts = recordsMap.get(t.getId());
                if (memberTopicsPosts != null && memberTopicsPosts.size() > 0) {
                    //设置围观人数
                    vo.setOnLookers(memberTopicsPosts.size());
                    //设置累计点赞数
                    Integer thumbs = memberTopicsPosts.stream().map(MemberTopicsPost::getThumbs).reduce(0, Integer::sum);
                    vo.setThumbs(thumbs);
                } else {
                    vo.setOnLookers(0);
                    vo.setThumbs(0);
                }
                Integer topicStatus = memberTopicsRepository.getTopicStatus(t);
                vo.setTopicStatus(topicStatus);
                return vo;
            }).collect(Collectors.toList());

        }
        PageResult<TopicListVO> pageResult = PageResult.empty();
        pageResult.setTotal(page.getTotal());
        pageResult.setList(collect);
        return pageResult;
    }

    @Override
    public Boolean updateStatus(TopicUpdateStatusVO topicUpdateStatusVO) {
        MemberTopics memberTopics = baseMapper.selectById(topicUpdateStatusVO.getId());
        if (ObjectUtils.isEmpty(memberTopics)){
            throw exception(TOPIC_NOT_EXIST);
        }
        if (memberTopics.getStatus()==topicUpdateStatusVO.getStatus()){
            throw exception(TOPIC_STATUS_ERROR);
        }
        return  memberTopicsRepository.updateTopicStatus(topicUpdateStatusVO);
    }

}
